package com.copa.l617;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author copa
 * @createDate 2022-08-09 09:38
 * @function 广度优先遍历
 */
public class Test1 {

    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null && root2 == null) {
            // 如果两者都没有，直接返回null
            return null;
        }
        if (root1 == null) {
            // 如果root1没有，直接返回root2
            return root2;
        }
        if (root2 == null) {
            // 如果root2没有，直接返回root1
            return root1;
        }
        // 合并后的二叉树
        TreeNode result = new TreeNode(root1.val + root2.val);
        // 合并后的二叉树队列
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(result);
        // root1的二叉树队列
        Queue<TreeNode> queue1 = new LinkedList<>();
        queue1.add(root1);
        // root2的二叉树队列
        Queue<TreeNode> queue2 = new LinkedList<>();
        queue2.add(root2);
        // 当这两树遍历完了就证明已经合并完成
        while (queue1.size() > 0 && queue2.size() > 0) {
            TreeNode node = queue.poll();
            TreeNode node1 = queue1.poll();
            TreeNode node2 = queue2.poll();
            TreeNode left1 = node1.left;
            TreeNode left2 = node2.left;
            TreeNode right1 = node1.right;
            TreeNode right2 = node2.right;
            if (left1 != null || left2 != null) {
                // 若左子树至少一个树有东西
                if (left1 != null && left2 != null) {
                    // 若左子树两个树有东西
                    node.left = new TreeNode(left1.val + left2.val);
                    queue.add(node.left);
                    queue1.add(node1.left);
                    queue2.add(node2.left);
                } else if (left1 != null) {
                    // 若左子树left1有东西，直接引用left1即可
                    node.left = left1;
                } else if (left2 != null) {
                    // 若左子树left2有东西，直接引用left2即可
                    node.left = left2;
                }
            }
            if (right1 != null || right2 != null) {
                // 若右子树至少一个树有东西
                if (right1 != null && right2 != null) {
                    // 若右子树两个树有东西
                    node.right = new TreeNode(right1.val + right2.val);
                    queue.add(node.right);
                    queue1.add(node1.right);
                    queue2.add(node2.right);
                } else if (right1 != null) {
                    // 若右子树right1有东西，直接引用right1即可
                    node.right = right1;
                } else if (right2 != null) {
                    // 若右子树right2有东西，直接引用right2即可
                    node.right = right2;
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        TreeNode root1 = new TreeNode(1);
        root1.left = new TreeNode(3);
        root1.right = new TreeNode(2);
        root1.left.left = new TreeNode(5);
        TreeNode root2 = new TreeNode(2);
        root2.left = new TreeNode(1);
        root2.right = new TreeNode(3);
        root2.left.right = new TreeNode(4);
        root2.right.right = new TreeNode(7);
        TreeNode result = new Test1().mergeTrees(root1, root2);
        if (result == null) {
            System.out.println("没有树节点");
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(result);
        while (queue.size() > 0) {
            int size = queue.size();
            while (size > 0) {
                TreeNode node = queue.poll();
                System.out.print(node.val + "\t");
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
                size--;
            }
        }
    }
}
